package com.ruoyi.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.admin.domain.Paper;
import com.ruoyi.admin.domain.UserPaper;
import com.ruoyi.admin.model.request.CheckUserPaperReq;
import com.ruoyi.admin.model.request.PaperReq;
import com.ruoyi.admin.service.PaperService;
import com.ruoyi.admin.service.UserPaperService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomainV2;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * (Paper)表控制层
 *
 * @author makejava
 * @since 2023-04-06 16:06:35
 */
@RestController
@RequestMapping("system/paper")
public class PaperController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private PaperService paperService;

    @Autowired
    private UserPaperService userPaperService;

    /**
     * 分页查询所有数据
     *
     * @param paper 查询实体
     * @return 所有数据
     */
    @GetMapping("page")
    public TableDataInfo page(PageDomainV2 page, Paper paper) {
        return getDataTable(this.paperService.paperPage(buildPage(), paper));
    }

    /**
     * 保存试卷
     * @param paperReq
     * @return
     */
    @PreAuthorize("@ss.checkRole('admin|teacher')")
    @PostMapping("save")
    public AjaxResult save(@RequestBody PaperReq paperReq){
        return AjaxResult.success(this.paperService.savePaper(paperReq));
    }

    /**
     * 试卷详情
     * @param paperId
     * @return
     */
    @PreAuthorize("@ss.checkRole('admin|teacher|student')")
    @GetMapping("paperDetail")
    public AjaxResult paperDetail(Long paperId){
        return AjaxResult.success(this.paperService.paperDetail(paperId));
    }

    /**
     * 分页查询用户提交试卷
     * @param paperId
     * @return
     */
    @PreAuthorize("@ss.checkRole('admin|teacher|student')")
    @GetMapping("userPaperPage")
    public TableDataInfo userPaperPage(PageDomainV2 page, Long paperId){
        return getDataTable(userPaperService.page(buildPage(), new LambdaQueryWrapper<UserPaper>()
                .eq(UserPaper::getPaperId, paperId)
        ));
    }

    /**
     * 查看用户试卷提交详情
     * @param userPageId
     * @return
     */
    @PreAuthorize("@ss.checkRole('admin|teacher|student')")
    @GetMapping("userPaperDetail")
    public AjaxResult userPaperDetail(Long userPageId){
        return AjaxResult.success(this.userPaperService.userPaper(userPageId));
    }

    /**
     * 阅卷
     * @param checkUserPaperReq
     * @return
     */
    @PreAuthorize("@ss.checkRole('admin|teacher')")
    @PostMapping("checkUserPaper")
    public AjaxResult checkUserPaper(@RequestBody CheckUserPaperReq checkUserPaperReq){
        return AjaxResult.success(userPaperService.checkUserPaper(checkUserPaperReq));
    }

}

